package com.moji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;


import com.moji.entity.MjBasicInfo;
import com.moji.entity.MjHomepage;
import com.moji.entity.vo.CommonHomePageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moji.mapper.MjBasicInfoMapper;
import com.moji.mapper.MjHomepageMapper;
import com.moji.service.MjHomePageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 首页相关 服务实现类
 * </p>
 *
 * @author myg
 * @since 2024-07-23
 */
@Service
public class MjHomepageServiceImpl extends ServiceImpl<MjHomepageMapper, MjHomepage> implements MjHomePageService {
    @Resource
    private MjHomepageMapper mjHomepageMapper;

    @Override
    public List<MjHomepage> queryByPage(CommonHomePageDTO dto) {
        LambdaQueryWrapper<MjHomepage> queryWrapper = new LambdaQueryWrapper<>();
        if (dto.getType() != null) {
            queryWrapper.eq(MjHomepage::getType, dto.getType());
        }
        List<MjHomepage> page = this.list(queryWrapper);
        return page;
    }

    @Override
    public MjHomepage insert(MjHomepage mjBasicInfo) {
        LambdaQueryWrapper<MjHomepage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MjHomepage::getType, mjBasicInfo.getType());
        List<MjHomepage> mjHomepages = mjHomepageMapper.selectList(queryWrapper);
        if (mjHomepages != null && mjHomepages.size() > 0) {
            mjBasicInfo.setTypeTitle(mjHomepages.get(0).getTypeTitle());
            mjBasicInfo.setTypeSubtitle(mjHomepages.get(0).getTypeSubtitle());
        }

        this.mjHomepageMapper.insert(mjBasicInfo);
        return mjBasicInfo;
    }

    @Override
    public MjHomepage update(MjHomepage mjBasicInfo) {
        LambdaQueryWrapper<MjHomepage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MjHomepage::getId, mjBasicInfo.getId());
        mjBasicInfo.setTypeTitle(this.queryById(mjBasicInfo.getId()).getTypeTitle());
        mjBasicInfo.setTypeSubtitle(this.queryById(mjBasicInfo.getId()).getTypeSubtitle());
        this.mjHomepageMapper.update(mjBasicInfo, queryWrapper);
        return this.queryById(mjBasicInfo.getId());
    }

    @Override
    public boolean deleteById(Long id) {
        return this.mjHomepageMapper.deleteById(id) > 0;
    }

    @Override
    public MjHomepage queryById(Long id) {
        return this.mjHomepageMapper.selectById(id);
    }
}
